<template>
  <div class="el-card cardBorder">
    <div class="operate">
      <div class="operate">
        <el-button type="primary" @click="addRelationMapping">添加</el-button>
        <el-select v-model="searchParams.userFirstId" placeholder="请选择用户一" style="margin: 0 5px">
          <el-option v-for="(item, index) in students" :key="index" :label="item.nickName"
                     :value="item.userId"></el-option>
        </el-select>
        <el-select v-model="searchParams.userSecondId" placeholder="请选择用户二" style="margin: 0 5px">
          <el-option v-for="(item, index) in students" :key="index" :label="item.nickName"
                     :value="item.userId"></el-option>
        </el-select>
        <el-select v-model="searchParams.relationId" placeholder="请选择关系" style="margin: 0 5px">
          <el-option v-for="(item, index) in relations" :key="index" :label="item.relationName"
                     :value="item.relationId"></el-option>
        </el-select>
        <el-button type="primary" @click="getAllRelationMapping">搜索</el-button>
      </div>
    </div>
    <el-table
        :data="relationMappings"
        border
        style="width: 100%;height: 100%;margin-bottom: 10px">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="firstUser.nickName" label="用户一类型">
          <template v-slot="scope">
              <span v-if="scope.row.firstUser.userType==='1'">教师</span>
            <span v-if="scope.row.firstUser.userType==='2'">学生</span>
          </template>
      </el-table-column>
      <el-table-column prop="firstUser.nickName" label="用户一"></el-table-column>
      <el-table-column prop="secondUser.nickName" label="用户二类型">
        <template v-slot="scope">
          <span v-if="scope.row.secondUser.userType==='1'">教师</span>
          <span v-if="scope.row.secondUser.userType==='2'">学生</span>
        </template>
      </el-table-column>
      <el-table-column prop="secondUser.nickName" label="用户二"></el-table-column>
      <el-table-column prop="relation.relationName" label="关系"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchParams.pageNum"
        :page-size="searchParams.pageSize"
        :page-sizes="[5, 8, 10, 20]"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
    <relation-mapping-dialog :visible.sync="addDialog" :students="students" :relations="relations" @addResult="getAllRelationMapping"></relation-mapping-dialog>
    <realtion-mapping-update-dialog :visible.sync="updateDialog" :relationMapping="updateRelationMapping" :students="students" :relations="relations" @addResult="getAllRelationMapping"></realtion-mapping-update-dialog>
  </div>
</template>

<script>
import {deleteRelationMapping, getAllRelation, getAllRelationMapping} from "@/api/relation";
import {getAllUser} from "@/api/user";
import RelationMappingDialog from "@/components/dialog/relationMappingDialog";
import RealtionMappingUpdateDialog from "@/components/dialog/RealtionMappingUpdateDialog";

export default {
  name: "relationResult",
  components: {RealtionMappingUpdateDialog, RelationMappingDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 5,
        relationId:'',
        userFirstId:'',
        userSecondId:''
      },
      relationMappings:[],
      students:[],
      relations:[],
      total:10,
      addDialog:false,
      updateRelationMapping:{},
      updateDialog:false
    }
  },
  methods:{
    getAllRelationMapping() {
      getAllRelationMapping(this.searchParams).then(res=>{
        this.relationMappings=res.data.records;
      })
    },
    handleSizeChange(val){
      this.getAllRelationMapping();
    },
    handleCurrentChange(val){
      this.getAllRelationMapping();
    },
    handleEdit(index,row){
      this.updateRelationMapping={id:row.id,userFirstId:row.firstUser.id,userSecondId:row.secondUser.id,relationId:row.relation.id};
      this.updateDialog=true;
    },
    handleDelete(index,row){
      deleteRelationMapping(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功退选")
          this.getAllRelationMapping();
        }
      })
    },
    addRelationMapping(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllRelationMapping();
    getAllUser().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].userType!=='0'){
            this.students.push({'userId':res.data[i].id,'nickName':res.data[i].nickName})
          }
        }
      }
    });
    getAllRelation().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
          this.relations.push({'relationId':res.data[i].id,'relationName':res.data[i].relationName})
        }
      }
    })
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
  .operate{
    margin: 16px;
  }
}
</style>
